Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error if struct field name is greater than 30 chars #1239

Merged
merged 3 commits into from
Mar 5, 2024
Merged

Conversation

leighmcculloch
Copy link
Member

@leighmcculloch leighmcculloch commented Mar 3, 2024

What

Raise compile error if struct field name is greater than 30 chars, instead of 32 chars.

Why

As it happens the contract spec restricts the length of the struct field name to 30 characters. This is an oversight. The type is a StringM<30>, but we should have made it a ScSymbol in the XDR so that it picked up the max length of a Symbol. We did this with other parts of the spec, but not this one it seems.

It's probably too late to change the type in a backwards compatible way, at least now. And there isn't significant value in trying to fix that because struct fields over 30 characters long but still under 32 characters long are rare.

The code change introduces a constant in the code that matches the const generic expected on the StringM type. This probably looks a little odd but is done to introduce a compiler check/guarantee that the length of the value we print out can never diverge from the max size of the StringM that stores the value. If the XDR ever changes to use a larger value, this code in the SDK will fail to compile until we update it.

Close #1228

Other Problems This Creates

This discrepancy does create some problems. It's possible using the raw Map type to store key-value pairs where the keys are Symbols up to 32-bytes long, that cannot be transferred into a contracttype because they cannot have field names greater than 30-bytes long. This is quite an edge case, and so unlikely to be a problem someone encounters, but it is unfortunate.

Example

Before

No error. An empty field name is written to spec XDR.

After

error: struct field name is too long: 31, max is 30
  --> tests/empty/src/lib.rs:16:9
   |
16 |     pub a234567890123456789001234567890: u64,
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@leighmcculloch leighmcculloch enabled auto-merge March 3, 2024 23:39
@leighmcculloch leighmcculloch requested review from graydon and removed request for sisuresh March 3, 2024 23:40
@leighmcculloch leighmcculloch added this pull request to the merge queue Mar 5, 2024
Merged via the queue into main with commit d492a17 Mar 5, 2024
21 checks passed
@leighmcculloch leighmcculloch deleted the i1228 branch March 5, 2024 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setting a field name with length greater than limit generates invalid contract spec
2 participants